package com.kll.carolinehmibiz.presenter

import android.content.Context
import com.google.gson.Gson
import com.kll.carolinehmibiz.base.net.NetCode
import com.kll.carolinehmibiz.base.net.bean.BaseHttpResult
import com.kll.carolinehmibiz.base.net.rx.NetObserver
import com.kll.carolinehmibiz.base.utils.L
import com.kll.carolinehmibiz.base.utils.SpUtils
import com.kll.carolinehmibiz.model.AccountModel
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.CarolineNetHelper
import com.kll.carolinehmibiz.net.bean.*


/**
 * ====================================================
 * Created by Ziliang Cai on 2021/12/10 13:51
 * <a href="mailto:752777456@qq.com">Contact me</a>
 * ====================================================
 */
class AccountPresenter: BasePresenter {

    private constructor(context: Context): super(context){

    }

    companion object {

        private var mInstance: AccountPresenter? = null

        /**
         * 签名过期时间，建议不要设置的过短
         *
         *
         * 时间单位：毫秒
         * 默认时间：7 x 24 x 60 x 60 = 604800 = 7 天
         */
        const val EXPIRETIME = 2592000000

        fun getInstance(context: Context): AccountPresenter {
            if(mInstance == null)
                mInstance = AccountPresenter(context)
            return mInstance!!
        }

    }

    /**
     * 获取用户信息
     */
    fun getUserInfo(listener: OperateListener) {

        // 从缓存中获取
        var content = SpUtils.getInstance().getValueByString(mContext, "USER_INFO")
        if(content != null) {
            listener.onSuccess(NetCode.NET_CODE_OK, Gson().fromJson(content, GetUserInfoResponse::class.java))
        }

        // 从网络中获取
        CarolineNetHelper.getInstance(mContext).getUserInfo(object: NetObserver<BaseHttpResult<GetUserInfoResponse?>>(){
            override fun onSuccess(t: BaseHttpResult<GetUserInfoResponse?>) {
                if(t.data != null) {
                    SpUtils.getInstance().saveValueToSharePerference(mContext, "USER_INFO", Gson().toJson(t.data))
                }
                listener.onSuccess(t.status,  t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }

        })
    }

    /**
     * 修改用户信息
     */
    fun editUserInfo(userInfo: GetUserInfoResponse, listener: OperateListener) {

        CarolineNetHelper.getInstance(mContext).editUserInfo(userInfo, object: NetObserver<BaseHttpResult<EmptyResponse?>>() {
            override fun onSuccess(t: BaseHttpResult<EmptyResponse?>) {
                if(t.message == "success") {
                    SpUtils.getInstance().saveValueToSharePerference(mContext, "USER_INFO", Gson().toJson(userInfo))
                }

                listener.onSuccess(t.status, t.message)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }
        })
    }

    /**
     * 获取设备激活状态
     */
    fun getDeviceStatus(listener: OperateListener) {

        var status = AccountModel.getInstance(mContext).getDeviceActiveStatus()

        if(status == -1) {
            CarolineNetHelper.getInstance(mContext).getDeviceActiveStatus( object: NetObserver<BaseHttpResult<GetActiveStatusResponse>>() {
                override fun onSuccess(t: BaseHttpResult<GetActiveStatusResponse>) {

                    L.d("AccountPresenter","getDeviceStatus>>>onSuccess::${Gson().toJson(t)}")
                    AccountModel.getInstance(mContext).saveDeviceActiveStatus(t.data!!.isbind)
                    listener.onSuccess(t.status, t.data!!.isbind)
                }

                override fun onFail(code: Int, message: String) {

                    L.d("AccountPresenter","getDeviceStatus>>>onFail::$code")

                    listener.onFail(code, message)
                }
            })
        } else {
            listener.onSuccess(1, status)
        }
    }


    /**
     * 激活设备
     */
    fun activeDevice(mobile: String,  verifyCode: String, listener: OperateListener) {

        CarolineNetHelper.getInstance(mContext).activeDevice(mobile, verifyCode, object:
            NetObserver<BaseHttpResult<LoginResponse>>() {
            override fun onSuccess(t: BaseHttpResult<LoginResponse>) {

                AccountModel.getInstance(mContext).saveDeviceActiveStatus(1)

                //  保存登录态数据
                AccountModel.getInstance(mContext).saveLoginInfo(t.data)

                // 回调登录监听
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }
        })
    }

    /**
     * 获取UserSig
     */
    fun getUserSig(listener: OperateListener) {

//        var userSig = AccountModel.getInstance(mContext).getUserSig()
//
        if(AccountModel.getInstance(mContext).getUserInfo() != null) {
            CarolineNetHelper.getInstance(mContext).createUserSig("${AccountModel.getInstance(mContext).getUserInfo()!!.tencent_user_id}",
                EXPIRETIME, object : NetObserver<BaseHttpResult<GetCreateUserSig>>() {
                override fun onSuccess(t: BaseHttpResult<GetCreateUserSig>) {
                    if(t.data?.userSig !=  null && t.data?.sdkappid != null) {
                        AccountModel.getInstance(mContext).saveUserSig(t.data!!.userSig!!, t.data!!.sdkappid!!)
                    }

                    listener.onSuccess(t.status, t.data)
                }

                override fun onFail(code: Int, message: String) {
                    listener.onFail(code, message)
                }
            })
        }
//        else {
//            listener.onSuccess(NetCode.NET_CODE_OK, userSig)
//        }

    }

    /**
     * 获取IM房间号
     */
    fun getRoomId(callUserId: String, listener: OperateListener) {

        CarolineNetHelper.getInstance(mContext).getRoomId(callUserId, object : NetObserver<BaseHttpResult<GetRoomIdResponse>>(){
            override fun onSuccess(t: BaseHttpResult<GetRoomIdResponse>) {
                AccountModel.mRoomId = t.data!!.roomid.toInt()
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }

        })

    }

}